package com.wuji.salary.common;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.taobao.api.ApiException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;


/**
 * 获取 token 的配置类
 */
@Configuration
public class GetToken {

    static String token;

//    public static void main(String[] args) {
//        System.out.println(token);
//    }


    /**
     * 获取token 每小时执行一次
     */
    @Bean
    @Scheduled(cron = "0 0 0/1 * * ?")
    public static void getAccessToken(){
        //获取 accessToken
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
//        request.setAppkey("dingcyanen6tjmvpnbma");
        request.setAppkey("dingzczs2m9wpx8pvw7r");
//        request.setAppsecret("mjoYw_Jlb-xb61wAnPTPofXuzCBjHZpNiIiakOcoOl1fKZmKWUIulovioS4ZCMwo");
        request.setAppsecret("p_5Ods9rjBElnTPYISv2yCttzMa1DfNRp5w9tkllWLYlK44KbhAjn2AmR2okLWjX");
        request.setHttpMethod("GET");
        OapiGettokenResponse response = null;
        try {
            response = client.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(response.getBody());
        System.out.println(response.getAccessToken());
        token = response.getAccessToken();
        System.out.println(token);
    }
}
